package com.xiaoyun.seleniumlinux.config.driver;

import cn.hutool.core.io.FileUtil;
import io.github.bonigarcia.wdm.WebDriverManager;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;


/**
 * @author Xiaoyun461
 * @data 2022/5/12
 */
@Component
public class WebDriverConfig {


    private WebDriverManager webDriverManager;

    /**
     * 用户浏览器缓存位置
     */
    public static final String CHROME_CACHE_DIR = "selenium" + File.separator + "cache";

    /**
     * 用户浏览器数据位置
     */
    public static final String CHROME_DATA_DIR = "selenium" + File.separator + "data";

    /**
     * docker容器 浏览器数据位置
     */
    public static final String DOCKER_CHROME_CACHE_DIR = "/home/selenium/cache";

    /**
     * docker容器 浏览器数据位置
     */
    public static final String DOCKER_CHROME_DATA_DIR = "/home/selenium/data";

    private static final String USER_DIR = System.getProperty("user.dir");

    @PostConstruct
    public void init() throws Exception {
        String userDriverCache = USER_DIR + File.separator + CHROME_CACHE_DIR;
        String userDriverData = USER_DIR + File.separator + CHROME_DATA_DIR;
        //当不存在目录时,自动创建
        FileUtil.mkdir(userDriverCache);
        FileUtil.mkdir(userDriverData);

        webDriverManager = new ChromeDriverManager();
        ChromeOptions chromeOptions = new ChromeOptions();
        //禁用沙盒模式
        chromeOptions.addArguments("--no-sandbox");
        //无图
        chromeOptions.addArguments("--disable-dev-shm-usage");
        //禁用加载图片
        chromeOptions.addArguments("blink-settings=imagesEnabled=false");
        //禁用硬件加速
        chromeOptions.addArguments("--disable-gpu");
        //无头模式
        chromeOptions.addArguments("--headless=new");
        //浏览器数据目录
//        chromeOptions.addArguments("--user-data-dir=" + DOCKER_CHROME_DATA_DIR);
        //浏览器缓存目录
//        chromeOptions.addArguments("--disk-cache-dir=" + DOCKER_CHROME_CACHE_DIR);
        //隐藏滚动条
//        chromeOptions.addArguments("--hide-scrollbars");
        //隐身模式
//        chromeOptions.addArguments("--incognito");
        //禁用插件
//        chromeOptions.addArguments("--disable-plugins");
        //禁用Java
//        chromeOptions.addArguments("--disable-java");

        webDriverManager
                .capabilities(chromeOptions);
//                .browserInDocker()
//                .browserVersion("111.0")
//                .dockerStopTimeoutSec(1)
//                .dockerVolumes(
//                        userDriverCache + ":" + DOCKER_CHROME_CACHE_DIR,
//                        userDriverData + ":" + DOCKER_CHROME_DATA_DIR)
//                .dockerTimezone("Asia/Shanghai");

    }

    public WebDriver createWebDriver() {
        return webDriverManager.create();
    }

    public WebDriver createWebDriver(String proxy) {
        return webDriverManager.proxy(proxy).create();
    }

    public WebDriver createWebDriver(String proxy, String proxyUser) {
        return webDriverManager.proxy(proxy).proxyUser(proxyUser).create();
    }

    public WebDriver createWebDriver(String proxy, String proxyUser, String proxyPass) {
        return webDriverManager.proxy(proxy).proxyUser(proxyUser).proxyPass(proxyPass).create();
    }


    public void quit(WebDriver webDriver) {
        webDriverManager.quit(webDriver);
    }

    @PreDestroy
    public void destroy() throws Exception {
        webDriverManager.quit();
    }

}
